package org.hsy.java.juc.phaser;

import java.util.Random;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * @author heshiyuan
 * @date 2021/6/8 11:15
 */
public class MarryPhaser extends Phaser {
    static MarryPhaser phaser = new MarryPhaser();
    @Override
    protected boolean onAdvance(int phase, int registeredParties) {
        switch (phase) {
            case 0:
                System.out.println("所有人到齐了！");
                return false;
            case 1:
                System.out.println("所有人吃完了！");
                return false;
            case 2:
                System.out.println("所有人离开了！");
                System.out.println("婚礼结束！");
                return true;
            case 3:
                System.out.println("婚礼结束！新郎新娘抱抱！" + registeredParties);
                return true;
            default:
                return true;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        phaser.bulkRegister(16);
        for (int i = 0; i < 10; i++) {
            new Thread(new Person2("路人" + i)).start();
        }
        new Thread(new Person2("零号小三")).start();
        new Thread(new Person2("一号小三")).start();
        new Thread(new Person2("二号小三")).start();
        new Thread(new Person2("三号小三")).start();
        new Thread(new Person2("新郎")).start();
        new Thread(new Person2("新娘")).start();
    }

    static class Person2 implements Runnable{
        Random random = new Random();
        String name;
        public Person2(String name) {
            this.name = name;
        }

        public void arrive() {
            try {
                TimeUnit.MILLISECONDS.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.printf("%s 到达现场！\n", name);
            if("一号小三".equals(name)){
                phaser.arriveAndDeregister();
                System.out.printf("%s 看到现场后生气的离开了！\n", name);
            }
        }

        public void eat() {
            try {
                TimeUnit.MILLISECONDS.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if ("二号小三".equals(name)) {
                System.out.printf("%s 没吃饭生气的离开了！\n", name);
                phaser.arriveAndDeregister();
            }else{
                System.out.printf("%s 吃完!\n", name);
                if ("三号小三".equals(name)) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(random.nextInt(2000));
                    } catch (InterruptedException e) {
                    }
                    System.out.printf("%s 吃完饭，但是饭太难吃了，离开了！\n", name);
                    phaser.arriveAndDeregister();
                }else{
                    phaser.arriveAndAwaitAdvance();
                }
            }
        }
        public void leave () {
            try {
                TimeUnit.MILLISECONDS.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if("零号小三".equals(name)){
                System.out.printf("%s 大闹婚礼！\n", name);
                phaser.arriveAndAwaitAdvance();
            }else{
                System.out.printf("%s 离开！\n", name);
                phaser.arriveAndDeregister();
            }
        }
        private void hug () {
            if (name.equals("新郎") || name.equals("新娘")) {
                try {
                    TimeUnit.MILLISECONDS.sleep(random.nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.printf("%s 洞房！\n", name);
                phaser.arriveAndAwaitAdvance();
            } else {
                System.out.printf("%s 被强制离开，因为新郎新娘要爱爱！\n", name);
                phaser.arriveAndDeregister();
            }
        }

        @Override
        public void run(){
            arrive();
            eat();
            leave();
            hug();
        }
    }

}
